package com.stadium.controller;

import com.stadium.common.BaseResponse;
import com.stadium.common.enums.ResultEnum;
import com.stadium.common.utils.Md5Utils;
import com.stadium.dto.LoginDto;
import com.stadium.dto.SysUserDto;
import com.stadium.dto.WxLoginDto;
import com.stadium.service.SysLoginSV;
import com.stadium.service.SysUserSV;
import com.stadium.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/***
* @Description: 登录
* @Author: WeiShunli
* @Date: 2021/5/23
*/
@RestController
@RequestMapping("/api/loginmanagement")
@Api(tags = "登录管理")
public class LoginCL {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SysLoginSV sysLoginSV;
    @Autowired
    private SysUserSV sysUserSV;

    /***
    * @Description: 登录
    * @Param:
    * @return:
    * @Author: WeiShunli
    * @Date: 2021/5/23
    */
    @ApiOperation(value = "登录")
    @PostMapping("/v1/login")
    public BaseResponse<LoginDto> login(@Validated @RequestBody LoginVo loginVO) {
        try {
            SysUserDto sysUserDto = sysUserSV.getUserByLoginName(loginVO.getLoginName());
            if (sysUserDto != null) {
                if (sysUserDto.getPassword().equals(Md5Utils.hash(loginVO.getPassword()))) {
                    if (sysUserDto.getStatusCode().equals("1")) {
                        return BaseResponse.error(ResultEnum.ACCOUNT_FORBIDDEN);
                    } else {
                        LoginDto loginDto = sysLoginSV.login(loginVO, sysUserDto);
                        return BaseResponse.ok(loginDto);
                    }
                } else {
                    return BaseResponse.error(ResultEnum.PASSWORD_ERROR);
                }
            } else {
                return BaseResponse.error(ResultEnum.ACCOUNT_NOTEXIST);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return BaseResponse.error(ResultEnum.UNKNOWN_ERROR);
        }
    }


    @ApiOperation(value = "注册")
    @PostMapping("/v1/register")
    public BaseResponse register(@Validated @RequestBody RegisterVo registerVo) {
        try {
            sysUserSV.register(registerVo);
            return BaseResponse.ok();
        } catch (Exception e) {
            return BaseResponse.error(e.getMessage());
        }
    }

}
